/** @format */

import {service} from '../util/request';
import {to} from '../util';

type UrlParams = [string, 'get' | 'post' | 'put' | 'delete'];

interface IAdminApis {
  getCategory: UrlParams;
  deleteArticle: UrlParams;
}

// 管理端交易
const adminApis: IAdminApis = {
  getCategory: ['getCategory', 'get'],
  deleteArticle: ['deleteArticle', 'delete'],
};

interface IData {
  [propName: string]: any;
}

interface IDataAndParams {
  data?: IData | undefined;
  params?: IData | undefined;
}

interface IParams {
  url: string;
  method: 'get' | 'post' | 'delete' | 'put';
  data?: IData | undefined;
  params?: IData | undefined;
}

const trans = (transName: keyof IAdminApis, data: IDataAndParams = {}) => {
  const paramsRes: IParams = {
    url: `/${adminApis[transName][0]}`,
    method: adminApis[transName][1],
    data: data.data,
    params: data.params,
  };

  return to(service(paramsRes));
};

const adminTrans = (transName: keyof IAdminApis, data: IDataAndParams = {}) => {
  const paramsRes: IParams = {
    url: `/admin/${adminApis[transName][0]}`,
    method: adminApis[transName][1],
    data: data.data,
    params: data.params,
  };

  return to(service(paramsRes));
};

export {trans, adminTrans};
